package com.leo.utils;

import android.content.SharedPreferences;

/**
 * Description :  SharedPreferences辅助类
 * Creator     :  leo
 * Date        :  16/1/7   下午2:58
 */
public class PreferencesHelper {

    private static PreferencesHelper sInstance = new PreferencesHelper();

    private static String sLastSharedPreferencesName = null;

    private SharedPreferences mSharePreferences = null;

    private PreferencesHelper() {
    }

    /**
     * function: 取得实例
     *
     * @return
     * @ author:leo 2014年7月15日 下午5:13:00
     */
    public static PreferencesHelper getInstance() {
        return getInstance(null);
    }

    /**
     * 通过名称取得实例
     *
     * @param sharedPreferencesName
     * @return
     */
    public static PreferencesHelper getInstance(String sharedPreferencesName) {
        if (sInstance == null) {
            sInstance = new PreferencesHelper();
        }
        return sInstance;
    }


    /**
     * 获取String类型
     *
     * @param key
     * @return
     */
    public String getString(String key) {
        return mSharePreferences.getString(key, "");
    }

    /**
     * 获取String类型
     *
     * @param key
     * @param def
     * @return
     */
    public String getString(String key, String def) {
        return mSharePreferences.getString(key, def);
    }

    /**
     * 获取float类型
     *
     * @param key
     * @return
     */
    public float getFloat(String key) {
        return mSharePreferences.getFloat(key, 0f);
    }

    /**
     * 获取float类型
     *
     * @param key
     * @param def
     * @return
     */
    public float getFloat(String key, float def) {
        return mSharePreferences.getFloat(key, def);
    }

    /**
     * 获取int类型
     *
     * @param key
     * @return
     */
    public int getInt(String key) {
        return mSharePreferences.getInt(key, 0);
    }

    /**
     * 获取int类型
     *
     * @param key
     * @param def
     * @return
     */
    public int getInt(String key, int def) {
        return mSharePreferences.getInt(key, def);
    }

    public long getLong(String key) {
        return mSharePreferences.getLong(key, 0);
    }

    public long getLong(String key, long def) {
        return mSharePreferences.getLong(key, def);
    }

    public boolean getBoolean(String key) {
        return mSharePreferences.getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean def) {
        return mSharePreferences.getBoolean(key, def);
    }

    public void putString(String key, String value) {
        SharedPreferences.Editor ed = mSharePreferences.edit();
        ed.putString(key, value);
        ed.commit();
    }

    public void putInt(String key, int value) {
        SharedPreferences.Editor ed = mSharePreferences.edit();
        ed.putInt(key, value);
        ed.commit();
    }

    public void putFloat(String key, float value) {
        SharedPreferences.Editor ed = mSharePreferences.edit();
        ed.putFloat(key, value);
        ed.commit();
    }

    public void putLong(String key, long value) {
        SharedPreferences.Editor ed = mSharePreferences.edit();
        ed.putLong(key, value);
        ed.commit();
    }

    public void putBoolean(String key, boolean value) {
        SharedPreferences.Editor ed = mSharePreferences.edit();
        ed.putBoolean(key, value);
        ed.commit();
    }

    /**
     * 清除
     */
    public void clear() {
        SharedPreferences.Editor ed = mSharePreferences.edit();
        ed.clear();
        ed.commit();
    }
}
